/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Button, TextArea,Text, useToast,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, useItemInputControl} from "../../common/base";
import {useMedia} from "use-media";
import {assertNamesUnique} from "../../common/eHelper";
import {EditStorageContext} from "../../StorageContext";
import {config设备概况, itemA下结论} from "./Conclusion";
import {itemA单特性} from "../../industrial/EditorFeature";
import repTypeToModelType, {RepTypeArr, RepTypeArrSub} from "../../OldRepType";

/**开启编制时的：默认值初始化操作。  若打印：本编辑器全部都不显示。
 * */
export const EntranceSetup =
React.forwardRef((
    {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
) => {
    const atPrint = useMedia('print');
    const toast = useToast();
    const doCheckNames = React.useCallback((verId: string) => {
        const result = assertNamesUnique([{value: rep?.tzFields},
            {value: config设备概况, type:'esnt'},
            {value:[...itemA下结论,  ...itemA单特性, ]},
            {value:['Projects','附页表', ]} ]);
        toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
    }, [rep,toast]);
    const {storage, setStorage} =React.useContext(EditStorageContext) as any;
    const getInpFilter = React.useCallback((par: any) => {
        const {_tblFixed, } =par||{};
        return {_tblFixed, };
    }, []);
    const {inp, setInp} = useItemInputControl({ref});
    //搜寻 下一阶段的要做的 报告类型: arr来自浏览器直接console的 赋值。
    const doSearchNewRepType = React.useCallback((arr: any[]) => {
        let newAll={};
        let counts=0;
        arr.forEach(({"1": reptype, "9": endcount}) => {
            const existReptp = repTypeToModelType(reptype);
            if(!existReptp){
                newAll={...newAll, [reptype]:""};
                counts++;
            }
        });
        console.log("候补下阶段要做类型: newAll=",newAll);
        toast({title: "搜寻完", subtitle: "发现"+counts+"个报告类型", intent: "success"});
    }, [rep,toast]);
    const findObsoleteRepType = React.useCallback((arr: any[]) => {
        const exsTypes = Object.keys(RepTypeArr);
        const oneYearTyps = arr.map((obj :any) => obj["1"]);
        const set1 = new Set(exsTypes);
        const set2 = new Set(oneYearTyps);
        const difference1 = [...set1].filter(element => !set2.has(element));
        console.log("报告类型一年都没被使用过: obsolete=",difference1);
        toast({title: "搜寻完", subtitle: "发现"+difference1.length+"个报告类型", intent: "success"});
    }, [rep,toast]);
    const onlySubRepType = React.useCallback((arr: any[]) => {
        const exsTypes = Object.keys(RepTypeArr);
        const subrTypes = Object.keys(RepTypeArrSub);
        const set1 = new Set(exsTypes);
        const set2 = new Set(subrTypes);
        const difference2 = [...set2].filter(element => !set1.has(element));
        console.log("分项报告类型剔除: =",difference2);
        toast({title: "搜寻完", subtitle: "发现"+difference2.length+"个报告类型", intent: "success"});
    }, [rep,toast]);
    if (atPrint) return null;
    else return (
        <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                             alone={alone} label={'初始化本报告，默认值配置等'}>
            {process.env.REACT_APP_TEST==='true' && <><div>
                <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
                <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
                <Text variant="h5">设置待测试表格的各列宽度：</Text>
                <TextArea value={storage?._tblFixed || ''} rows={2} onChange={e =>{
                              JSON.parse(e.currentTarget.value??'[]');
                              setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}); }}/>
            </div>
            </>}
            {process.env.REACT_APP_REP_TYPE_ADDING==='true' && <><div>
                <Button intent='primary' onPress={() => doSearchNewRepType([])}>最近一年之内的，一年最少出具1次报告的，那些报告类型</Button>
                <Button intent='primary' onPress={() => findObsoleteRepType([])}>已经做的报告类型，有哪些再最近一年之内的，一年都没出具1次报告</Button>
                <Button onPress={() => onlySubRepType([])}>分项报告类型剔除</Button>
            </div>
            </>}
        </InspectRecordLayout>
    );
});

/*候补的报告类型搜索：
doSearchNewRepType(arr[])另外需提取出：域外来的。
直接在浏览器console中，调试状态，点击按钮断点位置场景中，arr=直接赋值arr=[]；再运行：doSearchNewRepType();
可生成新的类型列表。
* */
